<?php

namespace app\sysuser\controller\trainer;

use app\common\controller\SysBackend;
use app\common\enums\TrainerStatusEnum;
use app\common\model\banxi\trainer\Trainer as TrainerModel;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/**
 * 课程字典
 */
class Trainer extends SysBackend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new TrainerModel();
    }

    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 分页Select查询
     * @return void
     * @throws DbException
     */
    public function listSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", 'searchText', 'selectInitValue']);
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        if (!empty($param['selectInitValue'])) {
            $param['query']['EQ_id'] = $param['selectInitValue'];
        }
        $param['query']["EQ_status"] = TrainerStatusEnum::E3;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListSelectPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    public function listAppointmentPage()
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", "trainerId", "courseFullType", "date"]);
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $trainerId = $param['trainerId'] ?? 0;
        $date = $param['date'] ?? '';
        $list = $this->model->getListAppointmentPage($trainerId, $date, $where, '0', $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }

    public function saveOrUpdate()
    {
        $param = $this->request->param();
        if (empty($param)) {
            $this->ok('请传入参数');
        }
        try {
            $this->model->saveOrUpdate($param);
        } catch (ValidateException|PDOException|\Exception $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /**
     * 启停用
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function enable(): void
    {
        $param = $this->request->param(['id' => 0]);
        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '请选择你要修改的学生'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->updateEnable($param['id']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }
}